package com.opensource.lzx.restartclient.netty.handler;

import com.opensource.lzx.proto.RequestContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

public class InitializedHandler extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        // netty心跳检测
        ch.pipeline().addLast(new IdleStateHandler(0, 0, 5L, TimeUnit.SECONDS));

        // protobuf 解码
        ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
        ch.pipeline().addLast(new ProtobufDecoder(RequestContext.getDefaultInstance()));

        // protobuf 编码
        ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
        ch.pipeline().addLast(new ProtobufEncoder());

        // 业务处理组件
        ch.pipeline().addLast(new HeartBeatHandler());
        ch.pipeline().addLast(new ServiceRegisterHandler());
    }
}
